package com.hehao.java_basic.thread;


/**
 * @author Damon
 * @date 2021/12/21
 */
public class ThreadStatus {
    /**
     * 1.等待join线程结束再运行
     */
//    public static void main(String[] args) {
//        System.out.println("main线程启动！");
//        Thread t = new Thread(()->{
//            System.out.println("t线程打印内容");
//        });
//        System.out.println("t线程启动！");
//        t.start();
//        try {
//            t.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("main线程终止！");
//    }

    /**
     * 2.中断线程，interrupt方法，isInterrupted检测线程状态
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t = new MyThread();
        t.start();
        Thread.sleep(1);
        t.interrupt();
        t.join();
        System.out.println("end");
    }

    private static class MyThread extends Thread {
        @Override
        public void run() {
            int n = 0;
            while (!isInterrupted()){
                n++;
                System.out.println("内层打印"+n);
            }
        }
    }

}
